home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / win / steph1b0.zip / TC_GRAPH.C < prev    next >
Text File  |  1994-10-28  |  4KB  |  143 lines

  1. /* TC_GRAPH.C */
  2.  
  3. /* Turbo C example screen save and restore functions for use
  4.    when using Steph in graphics modes. */
  5.  
  6. /* Supplied with Steph 1.0 */
  7.  
  8. /* The problem with the Turbo C getimage and putimage functions
  9.    from the graphics library is that they can only store images
  10.    of 64Kb size and less.  Some of the screen saves that you
  11.    need to do when using Steph in graphics modes are considerably
  12.    larger than 64K... */
  13.  
  14. /* The solution used here is to save the screen as a list of either
  15.    1, 2, 4 or 8 blocks, each less than 64K in size. */
  16.  
  17.  
  18. /* This structure stores pointers to screen blocks */
  19.  
  20. typedef struct {
  21.    char number;             /* number of blocks */
  22.    unsigned pixheight;      /* height of block in pixels */
  23.    void _far *pointer[8];   /* array of pointers to upto 8 blocks */
  24. }screen_store;
  25.  
  26.  
  27. void my_screen_save( void )
  28. {
  29.    /* TC can only store in 64K blocks, so... */
  30.  
  31.    /* ysize is the height of the area to be saved.
  32.       bsize is the height of the blocks we are going to save */
  33.  
  34.    screen_store *info;
  35.    unsigned ysize = _pix_to_save.y2 - _pix_to_save.y1 + 1;
  36.    unsigned bsize = ysize;
  37.  
  38.    unsigned thissize;
  39.  
  40.    char count;
  41.    unsigned isize;
  42.  
  43.    /* Create a screen_store structure and put a pointer to it
  44.       in .data */
  45.  
  46.    info = (screen_store *)malloc( sizeof( screen_store ) );
  47.    _pix_to_save.data = (char *)info;
  48.    if( info == NULL )
  49.       return;
  50.  
  51.    /* Try just one block to start with */
  52.    info->number = 1;
  53.  
  54.    while (
  55.       ( isize = imagesize( _pix_to_save.x1, _pix_to_save.y1,
  56.       _pix_to_save.x2, _pix_to_save.y1 + bsize - 1 ) )
  57.       == 0xFFFF )
  58.    {
  59.       /* Couldn't fit the block in 64K, so let's use twice as
  60.          many blocks, each half the size. */
  61.  
  62.       info->number = info->number * 2;
  63.       bsize = (bsize + 1) / 2;
  64.    }
  65.  
  66.    /* Now we know how many blocks we need to save. */
  67.  
  68.    /* Store the height of a block, this will be useful when
  69.       we restore them... */
  70.  
  71.    info->pixheight = bsize;
  72.  
  73.    /* Loop for each block to be saved... */
  74.  
  75.    for( count = 0; count < info->number; count++ )
  76.    {
  77.       thissize = bsize;      /* the block size */
  78.  
  79.       /* Make the last block smaller if necessary. */
  80.  
  81.       if( ((count+1)*bsize) > ysize )
  82.          thissize = ysize - count * bsize;
  83.  
  84.       /* Then allocate a block of far storage space, and put
  85.          the pointer in the block table. */
  86.  
  87.       isize = imagesize( _pix_to_save.x1,
  88.             _pix_to_save.y1 + count * bsize, _pix_to_save.x2,
  89.             _pix_to_save.y1 + count * bsize + thissize - 1 );
  90.  
  91.       info->pointer[count] = farmalloc( (unsigned long)isize );
  92.  
  93.       if( info->pointer[count] == (void _far *)NULL )
  94.       {
  95.          /* The allocation failed to allocate enough blocks, so exit */
  96.  
  97.          /* Tidy up all the blocks allocated so far. */
  98.  
  99.          while( count > 0 )
  100.             farfree( info->pointer[(--count)] );
  101.  
  102.          free( info );
  103.          _pix_to_save.data = NULL;
  104.          return;
  105.       }
  106.  
  107.       /* the allocation was OK so store the screen block */
  108.  
  109.       getimage(_pix_to_save.x1, _pix_to_save.y1 + count * bsize,
  110.             _pix_to_save.x2,_pix_to_save.y1 + count * bsize + thissize - 1,
  111.             info->pointer[count] );
  112.  
  113.       /* Now loop back until all the blocks have been saved. */
  114.    }
  115. }
  116.  
  117.  
  118.  
  119. void my_screen_restore( void )
  120. {
  121.    /* Restore the screen given a pointer to a screen_store structure. */
  122.  
  123.    screen_store *info = (screen_store *)_pix_to_save.data;
  124.    char count;
  125.  
  126.    /* Go through all the blocks and redraw each. */
  127.  
  128.    for( count = 0; count < info->number; count++ )
  129.    {
  130.       /* Restore the block and free the space it used. */
  131.  
  132.       putimage( _pix_to_save.x1, _pix_to_save.y1 + count * info->pixheight,
  133.             info->pointer[count], COPY_PUT  );
  134.  
  135.       farfree( info->pointer[count] );
  136.    }
  137.  
  138.    /* Free the space used for the screen_store structure. */
  139.    free( info );
  140. }
  141.  
  142.  
  143.